package com.yz.xyt.service.impl;

import com.alibaba.excel.EasyExcel;
import com.yz.xyt.componet.ClazzListener;
import com.yz.xyt.componet.MajorListener;
import com.yz.xyt.componet.StaffListener;
import com.yz.xyt.componet.StudentListener;
import com.yz.xyt.constant.HttpStatus;
import com.yz.xyt.domain.ResponseResult;
import com.yz.xyt.domain.vo.ClazzImportVo;
import com.yz.xyt.domain.vo.MajorImportVo;
import com.yz.xyt.domain.vo.StaffImportVo;
import com.yz.xyt.domain.vo.StudentImportVo;
import com.yz.xyt.service.IBaseDataImportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

@Service
public class BaseDataImportServiceImpl implements IBaseDataImportService {

    @Autowired
    MajorListener majorListener;

    @Autowired
    ClazzListener clazzListener;

    @Autowired
    StudentListener studentListener;

    @Autowired
    StaffListener staffListener;

    @Override
    public ResponseResult importMajor(MultipartFile file) {
        ResponseResult result = new ResponseResult();
        result.setCode(HttpStatus.SUCCESS);
        result.setMsg("导入专业成功");

        try {
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream,MajorImportVo.class,majorListener).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
            result.setCode(HttpStatus.ERROR);
            result.setMsg("导入专业异常");
        }
        return result;
    }

    @Override
    public ResponseResult importClazz(MultipartFile file) {
        ResponseResult result = new ResponseResult();
        result.setCode(HttpStatus.SUCCESS);
        result.setMsg("导入班级成功");

        try {
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream,ClazzImportVo.class,clazzListener).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
            result.setCode(HttpStatus.ERROR);
            result.setMsg("导入班级异常");
        }
        return result;
    }

    @Override
    public ResponseResult importStudent(MultipartFile file) {
        ResponseResult result = new ResponseResult();
        result.setCode(HttpStatus.SUCCESS);
        result.setMsg("导入学生成功");

        try {
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream,StudentImportVo.class,studentListener).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
            result.setCode(HttpStatus.ERROR);
            result.setMsg("导入学生异常");
        }
        return result;
    }

    @Override
    public ResponseResult importStaff(MultipartFile file) {
        ResponseResult result = new ResponseResult();
        result.setCode(HttpStatus.SUCCESS);
        result.setMsg("导入教职工成功");

        try {
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream,StaffImportVo.class,staffListener).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
            result.setCode(HttpStatus.ERROR);
            result.setMsg("导入教职工异常");
        }
        return result;
    }
}
